<div class="layui-form-item">
    <button class="layui-btn fabu" style="float:right;margin-left: 10px;margin-right: 10px;">发布频道</button>
    <button class="layui-btn add_" style="float:right;margin-left: 10px;margin-right: 10px;">新建频道</button>
</div>
<br>


<table class="layui-table">
    <colgroup>
        <col width="8%">
        <col width="12%">
        <col width="15%">
        <col width="5%">
        <col width="5%">
        <col width="10%">
    </colgroup>
    <thead>
    <tr>
        <th>ID</th>
        <th>频道名称</th>
        <th>所属标签</th>
        <th>频道状态</th>
        <th>频道排序</th>
        <th>操作</th>
    </tr>
    </thead>

    <tbody>
    <?php if (!empty($type['row'])): ?>
        <?php foreach ($type['row'] as $k => $v): ?>
            <tr>
                <td><?= $v['id'] ?></td>
                <td><?= $v['test_name'] ?></td>
                <td>
                    <?php foreach ($v['select'] as $kk => $vv): ?>
                        <?php if ($v['test_biaoqian'] == $vv['id']) {
                            echo $vv['name'];
                        } ?>
                    <?php endforeach; ?>
                </td>
                <td>
                    <?php if ($v['test_online'] == 2) : ?>
                        <span class="layui-bg-green">上线</span>
                    <?php else: ?>
                        <span class="layui-bg-red">下线</span>
                    <?php endif; ?>
                </td>
                <td><?php if ($v['test_sort'] == 999999999) {
                        echo 0;
                    } else {
                        echo $v['test_sort'];
                    } ?></td>
                <td>
                    <a class="layui-btn layui-btn-xs layui-btn-normal"
                       href="<?= base_url("word/television/edit_type") ?>?id=<?= $v['id'] ?>">编辑</a>
                    <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del_type(<?= $v['id'] ?>)">删除
                    </button>
                    <!--                    <button class="layui-btn layui-btn-xs layui-btn-container" onclick='test(<? //= //json_encode($v,true)?>//)'>测试</button> -->
                    <?php if ($v['test_online'] == 2) : ?>
                        <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="online(1,<?= $v['id'] ?>)">下线
                        </button>
                    <?php else: ?>
                        <button class="layui-btn layui-btn-xs" onclick="online(2,<?= $v['id'] ?>)">上线
                        </button>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
    <?php else: ?>
        <tr>
            <td colspan="7" rowspan="3" style="color: #00CC00;font-size: 25px;height:100px;text-align: center;">暂无数据
            </td>
        </tr>
    <?php endif; ?>
    </tbody>
</table>
<script>
    function test(data) {
        console.log(data);
        alert(data.id);
        alert(data.select.count);

        var html = "";
        html += '<form class="layui-form form" style="margin-top: 50px;">';
        // 下拉框
        html += '<div class="layui-form-item">';
        html += '<label class="layui-form-label">标签名称</label>';
        html += '<div class="layui-input-block" style="width: 350px;"> ';
        html += '<select name="city" class="hide_city">';
        html += '<option value="1">测试</option>';
        for (var i = 0; i <= data.select.count; i++) {
            html += '<option value="1">测试</option>';
        }
        html += '</select>';
        html += '</div>';
        html += '</div>';
        // 表单
        html += '<div class="layui-form-item">';
        html += '<label class="layui-form-label">分类名称</label>';
        html += '<div class="layui-input-block" style="width: 350px;">';
        html += '<input type="text" name="hide_name" placeholder="请输入分类名称" class="layui-input hide_name">';
        html += '</div>';
        html += '</div>';
        // 单选框
        html += '<div class="layui-form-item">';
        html += '<label class="layui-form-label">上线状态</label>';
        html += '<div class="layui-input-block">';
        html += '<input type="radio" name="sex" value="2" title="上线" checked>';
        html += '<input type="radio" name="sex" value="1" title="下线">';
        html += '</div>';
        html += '</div>';
        // 按钮
        html += '<div class="layui-form-item">';
        html += '<div class="layui-input-block">';
        html += '<button class="layui-btn">保存</button>';
        html += '</div>';
        html += '</div>';

        html += '</form>';
        html += '';

        layer.open({
            title: "新增分类",
            type: 1,
            area: ['500px', '350px'],
            content: html
        });
        layui.form.render('select');
        layui.form.render('radio');
    }

    function online(test_online, id) {
        layer.confirm("是否进行上下线操作？", function () {
            var url = "<?=base_url("word/television/online")?>";
            $.ajax({
                type: "post",
                data: {id: id,test_online:test_online},
                dataType: "json",
                url: url,
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg, {time: 1000}, function () {
                            window.location.reload()
                        })
                    } else {
                        layer.msg(res.msg, {time: 2000})
                    }
                }
            })
        });
    }

    function del_type(id) {
        layer.confirm("是否进行删除？", function (index) {
            var url = "<?=base_url("word/television/del_type")?>";
            $.ajax({
                type: "post",
                data: {id: id},
                dataType: "json",
                url: url,
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg, {time: 1000}, function () {
                            window.location.reload()
                        })
                    } else {
                        layer.msg(res.msg, {time: 2000})
                    }
                }
            })
        });
    }

    //发布分类
    $('.fabu').click(function () {
        var fa = "not_test";
        $.ajax({
            type: "post",
            data: {fa: fa},
            dataType: "json",
            url: "<?=base_url("word/television/fabu")?>",
            success: function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {time: 1000}, function () {
                        window.location.reload()
                    })
                } else {
                    layer.msg(res.msg, {time: 2000})
                }
            }
        })
    });

    //新增分类，把隐藏的表单显示出来
    $(".add_").click(function () {
        //加载弹出层
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.open({
                title: "新增分类",
                type: 1,
                area: ['500px', '350px'],
                content: content_add_type
            });
        });

        //获取标签
        $.ajax({
            type: "get",
            data: {},
            dataType: "json",
            url: "<?=base_url("word/television/biaoqian")?>",
            success: function (res) {
                if (res.code == 0) {
                    var arr = res.data;
                    $.each(arr, function (i, n) {
                        $('.hide_city').append("<option value='" + n.id + "'>" + n.name + "</option>");
                    });
                    layui.form.render('select');
                } else {
                    layer.msg(res.msg, {time: 2000});
                }
            }
        })

        //新增分类保存
        $(".hide_save").click(function () {
            var hide_name = $('.hide_name').val();
            var hide_city = $('.hide_city').val();
            var hide_password = $('.hide_password').val();
            var a = [hide_name, hide_city, hide_password];
            $.ajax({
                type: "post",
                data: {hide_name: hide_name, hide_city: hide_city, hide_password: hide_password},
                dataType: "json",
                url: "<?=base_url("word/television/save_type")?>",
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg, {time: 2000}, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {time: 2000});
                    }
                }
            })
        })

        //layui重新渲染下拉框
        layui.form.render('select');

    });

    //新增分类保存的弹出层
    var content_add_type = "<br/><form class=\"layui-form form\">" +
        "<div class=\"layui-form-item\">" +
        "<label class=\"layui-form-label\">所属标签</label>" +
        "<div class=\"layui-input-block\" style=\"width: 350px;\">" +
        "<select name=\"city\" class=\"hide_city\">" +
        "</select>" +
        "</div>" +
        "</div>" +
        "<div class=\"layui-form-item\">" +
        "<label class=\"layui-form-label\">频道名称</label>" +
        "<div class=\"layui-input-block\" style=\"width: 350px;\">" +
        "<input type=\"text\" name=\"hide_name\" placeholder=\"请输入名称\" class=\"layui-input hide_name\">" +
        "</div>" +
        "</div>" +
        //        "<div class=\"layui-form-item\">" +
        //        "<label class=\"layui-form-label\">分类排序</label>" +
        //        "<div class=\"layui-input-inline\">" +
        //        "<input type=\"text\" name=\"hide_password\" placeholder=\"请输入分类排序\" class=\"layui-input hide_password\" style=\"width: 350px;\">" +
        //        "</div>" +
        //        "</div>" +
        "<div class=\"layui-form-item\">" +
        "<label class=\"layui-form-label\"></label>" +
        "<div class=\"layui-input-inline\">" +
        "<button type=\"button\" class=\"layui-btn hide_save\" style=\"margin-left: 15px;\">保存</button>" +
        "</div>" +
        "</div>" +
        "</form>";
</script>